package com.ll.daipao.web;

import com.alibaba.fastjson.JSONObject;
import com.ll.daipao.common.Constant;
import com.ll.daipao.dao.MangerMapper;
import com.ll.daipao.dto.Manger;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ll on 2017/9/25.
 */
@Controller
public class LoginController {

    @Autowired
    MangerMapper mangerMapper;
    @RequestMapping(value="login", produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String login(HttpServletRequest request){
        JSONObject res = new JSONObject();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            res.put("suc",false);
            res.put("msg","账号密码不能为空");
            return res.toJSONString();
        }
        Map<String,String > map = new HashMap<String, String>();
        map.put("username",username);
        map.put("password",password);
        Manger manger = mangerMapper.selectMangerByAll(map);
        request.getSession().setAttribute(Constant.LOGIN_USER,manger);
        res.put("suc",true);
        res.put("msg","成功");
        return res.toJSONString();
    }

    @RequestMapping(value="main", produces = "text/json;charset=UTF-8")
    public String main(HttpServletRequest request){
        return "main";
    }



}
